package com.code.demo.ui.common;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.code.demo.R;


public class DialogNormal {

    private Dialog mDialog;
    private TextView dialog_title;
    private TextView dialog_message;
    private View dialog_line;
    public TextView positive;
    public TextView negative;

    public DialogNormal(Context context, String title, String message) {
        this(context, title, message, null, null);
    }

    public DialogNormal(Context context, String title, String message, String confirm) {
        this(context, title, message, confirm, null);
    }

    public DialogNormal(Context context, String title, String message, String confirm, String cancel) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.widget_dialog_normal, null);

        mDialog = new Dialog(context, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
        dialog_title = (TextView) view.findViewById(R.id.dialog_title);
        dialog_message = (TextView) view.findViewById(R.id.dialog_message);
        if (title == null || title.trim().equals("")) {
            dialog_title.setVisibility(View.GONE);
        } else {
            dialog_title.setVisibility(View.VISIBLE);
            dialog_title.setText(title);
        }

        dialog_message.setText(message);

        positive = (TextView) view.findViewById(R.id.confirm);
        negative = (TextView) view.findViewById(R.id.cancel);
        dialog_line = view.findViewById(R.id.line);

        dialog_line.setVisibility(View.VISIBLE);
        positive.setVisibility(View.VISIBLE);
        negative.setVisibility(View.VISIBLE);

        if (confirm != null && cancel != null) {
            //两个按钮都是自定义
            positive.setText(confirm);
            negative.setText(cancel);
        } else if (confirm != null && cancel == null) {
            //只有一个确定按钮
            dialog_line.setVisibility(View.GONE);
            negative.setVisibility(View.GONE);
            positive.setText(confirm);
        } else if (confirm == null && cancel == null) {
            //两个按钮都是默认的， 左边是取消，右边是确定
        }
    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
    }

}
